<?php

namespace App\Http\Resources\Trip\Orders;

use App\Trip\Train\TrainType;
use Carbon\Carbon;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use JsonSerializable;

class OrderTrainResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param Request $request
     *
     * @return array|Arrayable|JsonSerializable
     */
    public function toArray($request)
    {
        $train                    = parent::toArray($request);
        $start                    = Carbon::parse($train['start_at']);
        $arrive                   = Carbon::parse($train['arrive_at']);
        $train_type_name          = TrainType::getTrainTypeName($train['train_code']);
        $train["type"]            = [
            "name"    => $train_type_name,
            "name_uy" => TrainType::getTrainTypeNameTranslation($train_type_name, 'uy'),
        ];
        $train["start_date"]      = $start->toDateString();
        $train["start_time"]      = $start->format("H:i");
        $train["arrive_date"]     = $arrive->toDateString();
        $train["arrive_time"]     = $arrive->format("H:i");
        $train["start_time_diff"] = Carbon::now()->lessThan(Carbon::parse($train['start_at'])) ?
            Carbon::now()->diffInMinutes($train['start_at']) : 0;

        unset($train['start_at'], $train['arrive_at']);
        return $train;
    }
}
